package cn.com.sinux.spring.commons;

/**
 * Created by Administrator on 2015/9/6.
 */
public class TreeNodeHelper<T extends TreeNode> {

    private TreeNodeParentHelper<T> treeNodeParentHelper;
    private TreeNodeSeqNumberHelper<T> treeNodeSeqNumberHelper;
    private TreeNodeOperateHelper<T> treeNodeOperateHelper;
    private TreeNodeParentValidatorHelper<T> treeNodeParentValidatorHelper;

    public static interface TreeNodeParentHelper<T extends TreeNode>{
        public String getParentId(T t);
    }

    public static interface TreeNodeSeqNumberHelper<T extends TreeNode>{
        public int compare(T t1, T t2);
    }

    public static interface TreeNodeOperateHelper<T extends TreeNode>{
        public void doOperate(T t);
    }

    public static interface TreeNodeParentValidatorHelper<T extends TreeNode>{
        public boolean doValidate(T t);
    }

    public TreeNodeHelper(TreeNodeParentHelper<T> treeNodeParentHelper) {
        this.treeNodeParentHelper = treeNodeParentHelper;
    }

    public TreeNodeHelper(TreeNodeParentHelper<T> treeNodeParentHelper, TreeNodeSeqNumberHelper<T> treeNodeSeqNumberHelper) {
        this.treeNodeParentHelper = treeNodeParentHelper;
        this.treeNodeSeqNumberHelper = treeNodeSeqNumberHelper;
    }

    public TreeNodeHelper(TreeNodeOperateHelper<T> treeNodeOperateHelper) {
        this.treeNodeOperateHelper = treeNodeOperateHelper;
    }

    public TreeNodeHelper(TreeNodeParentValidatorHelper<T> treeNodeParentValidatorHelper) {
        this.treeNodeParentValidatorHelper = treeNodeParentValidatorHelper;
    }

    public TreeNodeHelper(TreeNodeParentValidatorHelper<T> treeNodeParentValidatorHelper, TreeNodeOperateHelper<T> treeNodeOperateHelper) {
        this.treeNodeParentValidatorHelper = treeNodeParentValidatorHelper;
        this.treeNodeOperateHelper = treeNodeOperateHelper;
    }

    public String getParentId(T t){
        return this.treeNodeParentHelper.getParentId(t);
    }

    public boolean sortEnable(){
        return this.treeNodeSeqNumberHelper != null;
    }

    public int compare(T t, T t2){
        return this.treeNodeSeqNumberHelper.compare(t, t2);
    }

    public void doOperate(T t){this.treeNodeOperateHelper.doOperate(t);}

    public boolean doValidate(T t){return this.treeNodeParentValidatorHelper == null || this.treeNodeParentValidatorHelper.doValidate(t);}
}
